﻿using System;
namespace CILADA.Engine
{
    public class TypeDependency : IComparable
    {
        #region Fields
        private readonly TypeInfo _typeInfo;
        private readonly RelationshipClassification _relClassification;
        #endregion

        #region Properties
        public TypeInfo TypeInfo { get { return _typeInfo; } }
        public RelationshipClassification Relationship { get { return _relClassification; } }
        #endregion

        #region Constructor
        public TypeDependency(TypeInfo typeInfo, RelationshipClassification relClassification)
        {
            _typeInfo = typeInfo;
            _relClassification = relClassification;
        }
        #endregion

        #region Equals, GetHashCode, and ToString overrides
        public override bool Equals(object obj)
        {
            if (null == obj || !this.GetType().Equals(obj.GetType()))
            {
                return false;
            }
            TypeDependency dep = (TypeDependency)obj;
            return this.Relationship.Equals(dep.Relationship) && this.TypeInfo.Equals(dep.TypeInfo);
        }

        public override int GetHashCode()
        {
            return _typeInfo.GetHashCode() + _relClassification.GetHashCode();
        }

        public override string ToString()
        {
            return _relClassification + " " + _typeInfo.ToString();
        }
        #endregion

        #region IComparable Members

        public int CompareTo(object obj)
        {
            TypeDependency other = (TypeDependency)obj;

            int relClassCompRes = other._relClassification.CompareTo(this._relClassification);
            if (0 == relClassCompRes)
            {
                return this._typeInfo.FullName.CompareTo(other._typeInfo.FullName);
            }
            else
            {
                return relClassCompRes;
            }
        }
        #endregion
    }
}
